<template>
  <div>
    <Table :where="where" :url="url" @dataChange="dataChange" ref="Table">
      <div slot="hd-btn">
        <el-button type="primary" @click="addUser">添加人员</el-button>
      </div>
      <el-table-column prop="account_name" fixed label="工号" align='center'>
      </el-table-column>
      <el-table-column prop="real_name" label="姓名" align='center'>>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="150" align='center'>>
      </el-table-column>
      <el-table-column prop="role_name" label="角色" align='center'>>
      </el-table-column>
      <el-table-column prop="user_status" label="状态" align='center'>>
      </el-table-column>
      <el-table-column prop="create_by" label="创建人工号" width="100" align='center'>
      </el-table-column>
      <el-table-column prop="update_by" label="更新人工号" width="100" align='center'>
      </el-table-column>
      <el-table-column label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.c_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.u_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align='center' width="250">
        <template slot-scope="scope">
          <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" @click="reset(scope.row)" type="warning">重置密码</el-button>
          <el-button size="mini" type="danger" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </Table>
    <Dialog @close="close" @confirm="confirm" :dialogISshow="dialogISshow" :options="options" title="添加管理员" labelWidth="140px">
    </Dialog>
  </div>
</template>

<script>
import { where, userDialog } from './form';
export default {
	data() {
		return {
			where,
			url: '/admin/userList',
			dialogISshow: false,
			options: userDialog,
			baseOptions: JSON.parse(JSON.stringify(userDialog)),
			operationType: '',
			auserid: null,
			roleTypeList: [],
			submitIng: false,
			useType: '',

		};
	},
	mounted() {
	},
	methods: {
		confirm(data) {
			let result = '';
			let fromData = JSON.parse(JSON.stringify(data));
			if (this.operationType === 'edit') {
				fromData.auserid = this.auserid;
				delete fromData.passwd;
				result = this.$http('/admin/modifyUser', fromData);
			} else {
				result = this.$http('/admin/addUser', fromData);
			}
			result
				.then(res => {
					if (res.code === 10200) {
						log.ok(res.msg);
						this._refresh();
					}
				})
				.catch(err => {

				});
			this.close();
		},
		close() {
			// this.options = activeOpt;
			this.clearn();
			this.dialogISshow = false;
		},
		reset(data) {
			this.operationType = 'reset';
			this.auserid = data.auserid;
			this.$msgbox({
				title: '提示',
				message: `改操作会重置用户【${data.real_name}】的密码为：123456，是否继续？`,
				showCancelButton: true,
				center: true,
			}).then(confirm => {
				this.$http('/admin/modifyUser', {
					auserid: data.auserid,
					type: 'reset',
				}).then(res => {
					if (res.code == 10200) {
						log.ok('密码重置成功');
					}
				});
			});
		},
		addUser() {
			this.dialogISshow = true;

		},
		dataChange(tableData) {

			let opt = tableData.roleList.map((item, i) => {
				return {
					value: item.aroleid,
					label: item.role_name,
				};
			});
			this.options.roleType.options = opt;
			this.options.roleType.default = tableData.roleList[0].aroleid;
			this.options.roleType.value = tableData.roleList[0].aroleid;
		},
		edit(item) {
			this.operationType = 'edit';
			this.auserid = item.auserid;
			this.fill(item);
			this.dialogISshow = true;
			this.useType = 'edit';
		},
		_edit() {
			this.$http('/admin/modifyUser', this.formData).then(res => {
				log.ok(res.msg);
				this.$refs.Table._search();
			});
		},
		del(item) {
			this.$msgbox({
				title: '提示',
				message: '删除后不可恢复，是否要删除！',
				showCancelButton: true,
				center: true,
			})
				.then(res => {
					this.$http('/admin/delUser', { auserid: item.auserid })
						.then(res => {
							log.ok(res.msg);
							this.$refs.Table._search();
						})
						.catch(err => {});
				})
				.catch(res => {});

		},
		clearn() {
			for (let key in this.options) {
				this.options[key].value = this.baseOptions[key].value;
				this.options[key].disabled = this.baseOptions[key].disabled;
				this.options[key].protect = this.baseOptions[key].protect;


			}
		},
		fill(data) {
			let options = this.options;
			options.userName.value = data.real_name;
			options.workNumber.value = data.account_name;
			options.phone.value = data.mobile;
			options.checkPass.protect = 1;
			options.passwd.protect = 1;
			options.roleType.value = data.aroleid;
		},
		disabled(ary = []) {
			let options = this.options;
			ary.forEach(key => {
				if (options.hasOwnProperty(key)) {
					options[key].disabled = true;
				}
			});
		},
		_refresh() {
			this.$refs.Table._search();
		},
	},
};
</script>
